<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Goods\Validator;

use Laminas\I18n\Translator\Translator;
use Laminas\Validator\AbstractValidator;

class ClassIdValidator extends AbstractValidator
{
    const NOT_EMPTY    = 'notEmpty';
    const HAVE_CLASS_ID= 'haveClassId';

    protected $messageTemplates = [];

    public function __construct($options = [])
    {
        $trans = new Translator();
        $this->messageTemplates = [
            self::NOT_EMPTY     => $trans->translate("请选择商品主分类"),
            self::HAVE_CLASS_ID => $trans->translate("商品主分类必须包含在已经勾选的分类之内")
        ];

        parent::__construct($options);
    }

    public function isValid($value, $context = null)
    {
        if ($value <= 0) {
            $this->error(self::NOT_EMPTY);
            return false;
        }

        if (empty($context['inClassId']) || !in_array($value, $context['inClassId'])) {
            $this->error(self::HAVE_CLASS_ID);
            return false;
        }

        return true;
    }
}